//
//  FourthStepController.m
//  GroceryStoryboards
//
//  Created by Jose Luis Lopez Redondo on 01/01/14.
//  Copyright (c) 2014 Jose Luis Lopez. All rights reserved.
//

#import "FourthStepController.h"

@interface FourthStepController ()

@end

@implementation FourthStepController

@synthesize priceTextField, priceString;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.priceString = @"";
}

- (IBAction)nextStep:(UIButton *)sender {
    
    [priceTextField resignFirstResponder];
    
    if ([self.parentViewController respondsToSelector:@selector(nextStep)])
        [self.parentViewController performSelector:@selector(nextStep)];
}

#pragma mark - TextField management

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
//    if ([textField.text length] > 0) {
    
        // We hide the keyboard
        [textField resignFirstResponder];
        
        if ([self.parentViewController respondsToSelector:@selector(priceSelected:)])
            [self.parentViewController performSelector:@selector(priceSelected:) withObject:textField.text];
        
        if ([self.parentViewController respondsToSelector:@selector(nextStep)])
            [self.parentViewController performSelector:@selector(nextStep)];
        
        return YES;
//    } else
//        return NO;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    
    NSNumber* candidateNumber;
    
    NSString* candidateString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    range = NSMakeRange(0, [candidateString length]);
    
    [numberFormatter getObjectValue:&candidateNumber forString:candidateString range:&range error:nil];
    
    if (([candidateString length] > 0) && (candidateNumber == nil || range.length < [candidateString length])) {
        
        return NO;
    }
    else
    {
        if([[candidateString componentsSeparatedByString:@","] count] > 1)
            if ([[[candidateString componentsSeparatedByString:@","] objectAtIndex:1] length] > 2)
                return NO;
        
        if ([string isEqualToString:@""])
            self.priceString = [textField.text substringToIndex:[textField.text length] - 1];
        else
            self.priceString = [textField.text stringByAppendingString:string];
        
        // Inform to the parent for displaying it
        if ([self.parentViewController respondsToSelector:@selector(typingPrice:)])
            [self.parentViewController performSelector:@selector(typingPrice:) withObject:self.priceString];
        return YES;
    }
}

#pragma mark Scroll Control

- (void)hideControls {
    [priceTextField resignFirstResponder];
}

- (void)showControls {
    [priceTextField becomeFirstResponder];
}

@end
